﻿#include "widget.h"
#include "./ui_widget.h"

#include <QDebug>
#include <QMessageBox>

#include <filesystem>
#include <iostream>
#include <string>

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);
    // Windows 环境下将动态库拷贝到当前目录下
    // Linux 环境下需要手动将动态库添加到环境变量中
#ifdef WIN32
    try
    {
        // 获取当前可执行文件所在目录
        std::filesystem::path base_dir = std::filesystem::current_path();

        // 根据是否为 64 位进程设置 DLL 目录
        bool is_64_bit = (sizeof(void *) == 8);
        std::filesystem::path dll_dir;
        if (is_64_bit)
        {
            dll_dir = base_dir / "..\\..\\..\\..\\lib\\Windows\\libdaq\\lib\\x64\\msvc2019";
        }
        else
        {
            dll_dir = base_dir / "..\\..\\..\\..\\lib\\Windows\\libdaq\\lib\\x86\\msvc2019";
        }

        // 检查源目录是否存在
        if (!std::filesystem::exists(dll_dir) || !std::filesystem::is_directory(dll_dir))
        {
            std::cout << "Source directory does not exist." << std::endl;
        }

        // 构建源 DLL 路径和目标 DLL 路径
        std::string source_dll_name = "libdaq-2.0.0.dll";
        std::filesystem::path source_dll_path = dll_dir / source_dll_name;
        std::filesystem::path dest_dll_path = base_dir / source_dll_name;

        // 复制动态库到编译目录
        std::filesystem::copy_file(source_dll_path, dest_dll_path, std::filesystem::copy_options::overwrite_existing);
        std::cout << "Copied " << dll_dir.string() << " to " << base_dir.string() << std::endl;
    }
    catch (const std::filesystem::filesystem_error &ex)
    {
        std::cout << "Failed to copy DLL: " << ex.what() << std::endl;
    }
#endif

    daq331m_ = (daq331m_ptr)malloc(sizeof(daq331m_t));
    memset(daq331m_, 0, sizeof(daq331m_t));
    if (!daq331m_init(daq331m_))
    {
        qDebug() << u8"未找到动态库";
    }
}

Widget::~Widget()
{
    daq331m_deinit(daq331m_);
    free(daq331m_);
    delete ui;
}

void Widget::on_DCPushButton_clicked()
{
    auto dac_channel = DAC1;
    daq331m_->stop_dac(daq331m_, dac_channel);
    auto dac_mode = DirectCurrent;
    daq331m_->config_dac_mode(daq331m_, dac_channel, dac_mode);
    /* 输出直流 v：10v 范围为0.000V - 15.000V */
    auto dac_dc_data = 10.0;
    daq331m_->config_dac_direct_current_data(daq331m_, dac_channel, dac_dc_data);
    if (daq331m_->start_dac(daq331m_, dac_channel))
    {
        QMessageBox::information(this, u8"提示", u8"通道1输出直流电压10V");
    }
}

void Widget::on_ACPushButton_clicked()
{
    auto dac_channel = DAC1;
    daq331m_->stop_dac(daq331m_, dac_channel);
    auto dac_mode = Wave;
    daq331m_->config_dac_mode(daq331m_, dac_channel, dac_mode);
    /* 输出波形为 vpp：5v frequent:100hz 的正弦波 */
    auto frequent = 100;
    auto vpp = 5.0;
    auto wave_data = std::vector<double>(1000);
    auto temp_f = 2 * 3.14159 / 1000;
    for (int i = 1; i <= 1000; i++)
    {
        wave_data[i - 1] = 0.5 * vpp * (sin(temp_f * i) + 1) * 0.273; // 这里的0.273是为了将数据转换为0-15V的范围
    }
    daq331m_->config_dac_wave_data(daq331m_, dac_channel, frequent, wave_data.data());
    if (daq331m_->start_dac(daq331m_, dac_channel))
    {
        QMessageBox::information(this, u8"提示", u8"通道1输出正弦波");
    }
}

void Widget::on_ConnectPushButton_clicked()
{
    daq331m_->delete_device(daq331m_);
    daq331m_->create_device(daq331m_);

#if defined(USE_RS485) && !defined(USE_TCP_CLIENT) && !defined(USE_MODBUS)
    daq331m_->use_serial_backend(daq331m_, R"(\\.\COM20)", 115200);
#elif defined(USE_TCP_CLIENT) && !defined(USE_RS485) && !defined(USE_MODBUS)
    daq331m_->use_tcp_client_backend(daq331m_, "192.168.1.100", 8000);
#elif defined(USE_MODBUS) && !defined(USE_RS485) && !defined(USE_TCP_CLIENT)
    daq331m_->use_modbus_backend(daq331m_, R"(\\.\COM20)", 115200, 1);
#endif

    // 初始化一个DAQ设备
    if (!daq331m_->initialize_device(daq331m_))
    {
        qDebug() << (u8"libdaq初始化设备失败");
    }
    else
    {
        qDebug() << (u8"libdaq初始化设备成功");
    }

    // 连接一个DAQ设备
    if (!daq331m_->connected_device(daq331m_))
    {
        qDebug() << (u8"libdaq连接设备失败");
    }
    else
    {
        qDebug() << (u8"libdaq连接设备成功");
    }
}
